package com.google.code.fluentjoiner.rss;

import com.google.common.base.Function;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.SyndFeedOutput;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;

public class RomeFunctions
{
    public static final Function<String, SyndFeed> STRING_TO_SYNDFEED =
        new Function<String, SyndFeed>()
        {
            @Override
            public SyndFeed apply(final String from)
            {
                try
                {
                    final SyndFeedInput syndFeedInput = new SyndFeedInput();
                    return syndFeedInput.build(new StringReader(from));
                }
                catch (final Exception exception)
                {
                    throw new RuntimeException("Exception thrown.", exception);
                }
            }
        };

    public static final Function<SyndFeed, String> SYNDFEED_TO_STRING =
        new Function<SyndFeed, String>()
        {
            @Override
            public String apply(final SyndFeed feed)
            {
                try
                {
                    final SyndFeedOutput syndFeedOutput = new SyndFeedOutput();
                    return syndFeedOutput.outputString(feed);
                }
                catch (final Exception exception)
                {
                    throw new RuntimeException("Exception thrown.", exception);
                }
            }
        };

    public static final Function<SyndFeed, Iterator<SyndEntry>> SYNDFEED_TO_ENTRIES =
        new Function<SyndFeed, Iterator<SyndEntry>>()
        {
            @Override
            public Iterator<SyndEntry> apply(final SyndFeed from)
            {
                return ((List<SyndEntry>)from.getEntries()).iterator();
            }
        };

    public static final Function<SyndEntry, String> SYNDENTRY_TO_LINK =
        new Function<SyndEntry, String>()
        {
            @Override
            public String apply(final SyndEntry from)
            {
                return from.getLink();
            }
        };
}